package com.dragon.www.smarthome.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dragon.www.smarthome.R;

import org.dragonnova.lamp.Lamp;
import org.dragonnova.lamp.LampController;

import java.util.List;

/**
 * HarerLampAdapter
 * Created by haojinyu on 10月27 027.
 */
public class HarerLampAdapter extends BaseAdapter {

    private Context mContext;
    private List<Lamp> mLamps;
    private LampController mLampController;

    public HarerLampAdapter(Context context, List<Lamp> list, LampController controller) {
        mContext = context;
        mLamps = list;
        mLampController = controller;
    }

    @Override
    public int getCount() {
        return mLamps.size();
    }

    @Override
    public Object getItem(int position) {
        return mLamps.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Lamp lamp = (Lamp) getItem(position);
        LampViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new HarerLampAdapter.LampViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.haier_lamp_view_item, parent, false);
            viewHolder.splitbutton = (ImageView) convertView.findViewById(R.id.splitbutton);
            viewHolder.tv_lamp_desc = (TextView) convertView.findViewById(R.id.tv_lamp_desc);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (LampViewHolder) convertView.getTag();
        }
        viewHolder.tv_lamp_desc.setText(String.format("%s-%s", String.valueOf(lamp.getGroup()), String.valueOf(lamp.getId())));
        if (lamp.getStatus() == Lamp.ON) {
            viewHolder.splitbutton.setActivated(true);
        } else if (lamp.getStatus() == Lamp.OFF) {
            viewHolder.splitbutton.setActivated(false);
        }

        viewHolder.splitbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("haier","tv_lamp_button");
                mLampController.control(lamp, lamp.getStatus() == Lamp.ON ? Lamp.OFF : Lamp.ON);
            }
        });
        return convertView;

    }

//    @Override
//    public View bindHolder(View convertView, ViewGroup parent, int position) {
//        final Device device = this.devices.get(position);
//        HarerLampAdapter.LampViewHolder viewHolder;
//        if (convertView == null) {
//            viewHolder = new HarerLampAdapter.LampViewHolder();
//            convertView = LayoutInflater.from(context).inflate(R.layout.haier_lamp_view_item, parent, false);
//            viewHolder.splitbutton = (ImageView) convertView.findViewById(R.id.splitbutton);
//            viewHolder.tv_lamp_desc = (TextView) convertView.findViewById(R.id.tv_lamp_desc);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (LampViewHolder) convertView.getTag();
//        }
//        viewHolder.tv_lamp_desc.setText(device.getDeviceName());
//        final TextView tv_lamp_desc = viewHolder.tv_lamp_desc;
////        viewHolder.splitbutton.SetOnChangedListener(new SlipButton.OnChangedListener() {
////
////			public void OnChanged(boolean CheckState) {
////                tv_lamp_desc.setActivated(CheckState);
////                Log.e("haier",CheckState+"");
////			}
////		});
//        viewHolder.splitbutton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Log.e("haier","tv_lamp_button");
//                if (deviceInfo != null) {
//                    if (Integer.parseInt(device.getequipNo().split("-")[0]) == box1) {
//                        if (deviceInfo[0].getRelayValues()[Integer.parseInt(device.getequipNo().split("-")[1]) - 1] == 0) {
//                            setAction(device, 1, box1);
//                            tv_lamp_desc.setActivated(true);
//                            view.setActivated(true);
//                        } else if (deviceInfo[0].getRelayValues()[Integer.parseInt(device.getequipNo().split("-")[1]) - 1] == 1) {
//                            setAction(device, 0, box1);
//                            tv_lamp_desc.setActivated(false);
//                            view.setActivated(false);
//                        }
//                    } else if (Integer.parseInt(device.getequipNo().split("-")[0]) == box2) {
//                        if (deviceInfo[1].getRelayValues()[Integer.parseInt(device.getequipNo().split("-")[1]) - 1] == 0) {
//                            setAction(device, 1, box2);
//                            tv_lamp_desc.setActivated(true);
//                            view.setActivated(true);
//                        } else if (deviceInfo[1].getRelayValues()[Integer.parseInt(device.getequipNo().split("-")[1]) - 1] == 1) {
//                            setAction(device, 0, box2);
//                            tv_lamp_desc.setActivated(false);
//                            view.setActivated(false);
//                        }
//                    }
//                }
//            }
//        });
//        return convertView;
//    }

    public class LampViewHolder extends AppHolder {
        public ImageView splitbutton;
        public TextView tv_lamp_desc;
    }


//    public void setAction(final Device lamp, int action, int boxid) {
//        NetHelper.sendAction(context, boxid, 1, Integer.parseInt(lamp.getequipNo().split("-")[1]), "power", action, "relay", new NetUtils.NetCallBack<DeviceInfo>() {
//
//            @Override
//            public void success(DeviceInfo rspData) {
//                if (rspData.getError() == 0) {
//
//                } else {
//                    String description = rspData.getDescription();
//                    Log.e("error", description);
//                }
//            }
//
//            @Override
//            public void failed(String msg) {
//                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
//            }
//        }, SingleReturnInfo.class);
//    }
}
